//
//  ViewController.m
//  socket_obj
//
//  Created by Alimjan on 2018/3/15.
//  Copyright © 2018年 Alimjan. All rights reserved.
//

#import "ViewController.h"
@import SocketIO;
#import "SocketIO.h"
#import "SocketIOPacket.h"


@interface ViewController ()<SocketIODelegate> {
    SocketIO *_socketIO;
    UITableView *_tableView;
    NSMutableArray<NSDictionary*> *_dataArr;
    SocketIOClient* socket;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     [self connect];
}

- (void)connect {
    NSURL* url = [[NSURL alloc] initWithString:@"http://192.168.1.109:6002"];
    socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @YES, @"forcePolling": @YES}];
    
    [socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"--------------------------------------socket connected");
    }];
    
    
    [socket on:@"sarang" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"------------ulinixulinixulinix--------------------------%@",data);
        
        [socket emit:@"ulinix" with:@[@"amount"]];
    }];
    //    [socket on:@"ulinix" callback:^(NSArray* data, SocketAckEmitter* ack) {
    //        NSLog(@"------------ulinixulinixulinix--------------------------socket connected");
    //    }];
    
    [socket on:@"currentAmount" callback:^(NSArray* data, SocketAckEmitter* ack) {
        double cur = [[data objectAtIndex:0] floatValue];
        
        [[socket emitWithAck:@"canUpdate" with:@[@(cur)]] timingOutAfter:0 callback:^(NSArray* data) {
            [socket emit:@"update" with:@[@{@"amount": @(cur + 2.50)}]];
        }];
        
        [ack with:@[@"Got your currentAmount, ", @"dude"]];
    }];
    [socket on:@"typing" callback:^(NSArray * _Nonnull a, SocketAckEmitter *d) {
        NSLog(@"%@ , %@",a,d);
    }];
    
    [socket on:@"new message" callback:^(NSArray * _Nonnull a, SocketAckEmitter * _Nonnull b) {
        NSLog(@" new message %@ , %@",a,b);
        [_dataArr addObject:a[0]];
        [_tableView reloadData];
        
        
    }];
    
    [socket connect];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
